﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace EwsRelentless
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.i
        /// </summary>
        [STAThread]
 

        public static void Main(string[] args)
        {
 
            bool bSuppressLogging = false;
            bool bLogAsCSV = false;
            string LogFileName = string.Empty;
            bool bError = false;

            if (args.Length == 0)
            {
 
                Console.WriteLine("Configuration file must be specified.");
            }
            else
            {

                if (args.Length >= 1 || args.Length <= 3)
                {
                    string sFile = args[0];

                    if (args.Length >= 2)
                    {
                        if (args[1].ToUpper() == "/SILENT") // SILENT
                        {
                            bSuppressLogging = true;
                        }
                        else
                        {
                            if (args[1].ToUpper() == "/LOGASCSV") // LogAsCSV
                            {
                                bSuppressLogging = true;
                                bLogAsCSV = true;

                                if (args.Length == 3)  // Have a filename?
                                {
 

                                    if (File.Exists(args[2]))
                                    {
                                        Console.WriteLine("Error The file specified log file already exists: {0}", args[2]);
                                        bError = true;
                                    }
                                    else
                                    {

                                        LogFileName = args[2];
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("Unknown Parameter: " + args[1]);
                                bError = true;
                            }
                        }
                    }

                    if (bError == false)
                    {

                        if (File.Exists(sFile) == true && bError == false) // Verify that the config file exists 
                        {
                            Console.WriteLine("Using configuration file: " + sFile);

                            List<EWSConfigItems> listEWSConfigItems = new List<EWSConfigItems>();
                            listEWSConfigItems = FileIoHelper.LoadConfigFile(sFile);

                            TextWriterTraceListener oLogListener = null;

                            if (listEWSConfigItems != null)
                            {
                                //FileStream oFileStream = null;
                                //StreamWriter oStreamWriter = null;

                                //if (LogFileName != string.Empty)
                                //{
                                     
                                //    oFileStream = new FileStream(LogFileName, FileMode.Create);
                                //    oStreamWriter = new StreamWriter(oFileStream);
                                //}
                                if (LogFileName != string.Empty)
                                {
                                    oLogListener = new TextWriterTraceListener(LogFileName, "LogListener");

                                }
 
                                ProcessConfigFile oPCF = new ProcessConfigFile();
                                oPCF.ProcessFile(sFile, bSuppressLogging, bLogAsCSV, oLogListener,  listEWSConfigItems);
 
                                if (LogFileName != string.Empty)
                                    oLogListener.Flush();

        
                            }
                            else
                            {
                                Console.WriteLine("");
                                Console.WriteLine("Error occurred in loading configuration file. Exiting application.");
                                System.Threading.Thread.Sleep(3000);
                            }
                        }
                        else
                        {
                            Console.WriteLine("The configuration file does not exist.");
                        }
                    }
                     
                }
 
            }
 
        }

 
    }
}
